<?php 
session_start();
require_once ("class/m_suppliers.php");

	$m = new M_suppliers();
	$user=$_SESSION['username'];
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				echo json_encode($m->getDataDetail($id));
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$user;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			if ($m->checkSupplierCode($ar["SupplierCode"])=="0"){
				echo "error";
			}else{
				
				$id=$m->InsertData($ar);
			 
				$ar["ID"]=$id;
				echo "<tr>".getTable($ar , $_POST['rows']+1)."</tr>"; 
			}
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);

				$ar["UpdatedBy"]=$user;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$sqlScript="select * from tb_supplier where IsActived=1 and ID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);
				
				$ar["ID"]=$_POST['id'];
				
				echo  getTable($ar ,$_POST['rows']);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"]);
			}
		}else if ($_POST['m']== 'search'){
			$code=$_POST['code'];
			$name=$_POST['name'];
			$term=$_POST['term'];
			$page=$_POST['page'];
			
			$page=isset($page)?$page:1;
			
			$result=$m->getData($code,$name,$term,$page);
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows>0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";    
					}
					$html .= getTable($row,$i)."</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan=9 align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";			
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
		}
	}else{echo"Error2";}

	function getTable($row,$id){
		global $supplierScreen;
		$html .="<td align='center'>".$id."</td>";
		$html .="<td align='center' style='display:none'>".$row['ID']."</td>";
		$html .="<td  align='center'>".$row['SupplierCode']."</td>";
		$html .="<td  align='center'>".$row['SupplierName']."</td>";
		$html .="<td  align='center'>".$row['Address']."</td>";
		$html .="<td  align='center'>".$row['Telephone']."</td>";
		$html .="<td  align='center'>".$row['ContactName']."</td>";
		$html .="<td  align='center'>".$row['CreditTerm']."</td>";
		$html .="<td align='center' style='display:none'><img src='./images/view.png' class='btnView' style='cursor:pointer'  title='View' border='0'/></td>";
		
		if ($_SESSION["permission"][$supplierScreen]["Edit"]=="1") 
			$html.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer'  title='Edit' border='0'/></td>";
		else
			$html.="<td>&nbsp;</td>";
		
		if ($_SESSION["permission"][$supplierScreen]["Deleted"]=="1") 		
			$html .="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer'  title='Delete' border='0'/></td>";
		else
			$html .="<td>&nbsp;</td>";
			
		return $html;
	}

?>